package tech.aistar.controller;

import tech.aistar.pojo.Phone;
import tech.aistar.pojo.PhoneDetails;
import tech.aistar.pojo.PhoneType;
import tech.aistar.service.IPhoneDetailsService;
import tech.aistar.service.IPhoneService;
import tech.aistar.service.IPhoneTypeService;
import tech.aistar.service.impl.PhoneDetailsServiceImpl;
import tech.aistar.service.impl.PhoneServiceImpl;
import tech.aistar.service.impl.PhoneTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Ray
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/9/11  12:10
 */
@WebServlet(urlPatterns = "/phone/details")
public class DetailsAction extends HttpServlet {
    private IPhoneTypeService phoneTypeService = new PhoneTypeServiceImpl();
    private IPhoneService phoneService = new PhoneServiceImpl();
    IPhoneDetailsService iPhoneDetailsService = new PhoneDetailsServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");

        Phone phone = phoneService.findById(Integer.valueOf(id));
        //将手机信息放到request作用域中
        req.setAttribute("phone",phone);


        List<PhoneType> phoneTypeList = phoneTypeService.findAll();
        req.setAttribute("phoneTypeList",phoneTypeList);

        PhoneDetails phoneDetails = iPhoneDetailsService.getByPhoneId(Integer.valueOf(id));
        req.setAttribute("phoneDetails",phoneDetails);

        req.getRequestDispatcher("/prj/details/details.jsp").forward(req,resp);
    }
}
